package crawlers;

import helpers.CrawlerHelper;

import java.util.ArrayList;

import models.Software;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.appengine.api.datastore.Text;

import debug.SoftwareDebug;

public class CrawlerItuneJson {
	public static ArrayList<Software> getJson(String url) {
		ArrayList<Software> softwares = new ArrayList<Software>();
		try {
			String content = CrawlerHelper.getContentFromUrl(url);
	        JSONObject json = new JSONObject(content);
	        JSONArray results = json.getJSONArray("results");
	        
	        Software software = new Software();
	        JSONObject jsonObj = new JSONObject();
	        for(int i = 0; i < results.length(); i++ ) {
	        	software = new Software();
	        	jsonObj = (JSONObject) results.get(i);
	        	//Set trackCensoredName
	        	software.setTrackCensoredName((String) jsonObj.getString("trackCensoredName"));
	        	//Set artworkUrl60
	        	software.setArtworkUrl60((String) jsonObj.getString("artworkUrl60"));
	        	//Set genres
	        	ArrayList<String> genres = new ArrayList<String>();
	        	JSONArray jsonGenres = jsonObj.getJSONArray("genres");
	        	for (int j = 0; j < jsonGenres.length(); j++) {
	        		genres.add((String) jsonGenres.get(j));
	        	}
	        	software.setGenres(genres);
	        	//Set wrapperType
	        	software.setWrapperType((String) jsonObj.getString("wrapperType"));
	        	//Set trackName
	        	software.setTrackName((String) jsonObj.getString("trackName"));
	        	//Set kind
	        	software.setKind((String) jsonObj.getString("kind"));
	        	//Set version
	        	software.setVersion((String) jsonObj.getString("version"));
	        	//Set currency
	        	software.setCurrency((String) jsonObj.getString("currency"));
	        	//Set releaseNotes
	        	try {
	        		software.setReleaseNotes(new Text(jsonObj.getString("releaseNotes")));
				} catch (Exception e) {
					// TODO: handle exception
				}
	        	
	        	//Set releaseDate
	        	software.setReleaseDate((String) jsonObj.getString("releaseDate"));
	        	//Set isGameCenterEnabled
	        	software.setIsGameCenterEnabled((Boolean) jsonObj.getBoolean("isGameCenterEnabled"));
	        	//Set artistId
	        	software.setArtistId((int) jsonObj.getInt("artistId"));
	        	//Set description
	        	software.setDescription(new Text(jsonObj.getString("description")));
	        	//Set features
	        	ArrayList<String> features = new ArrayList<String>();
	        	JSONArray jsonFeatures = jsonObj.getJSONArray("features");
	        	for (int j = 0; j < jsonFeatures.length(); j++) {
	        		features.add((String) jsonFeatures.get(j));
	        	}
	        	software.setFeatures(features);
	        	//Set primaryGenreId
	        	software.setPrimaryGenreId((int) jsonObj.getInt("primaryGenreId"));
	        	//Set contentAdvisoryRating
	        	software.setContentAdvisoryRating((String) jsonObj.getString("contentAdvisoryRating"));
	        	//Set genreIds
	        	ArrayList<String> genreIds = new ArrayList<String>();
	        	JSONArray jsonGenreIds = jsonObj.getJSONArray("genreIds");
	        	for (int j = 0; j < jsonGenreIds.length(); j++) {
	        		genreIds.add((String) jsonGenreIds.get(j));
	        	}
	        	software.setGenreIds(genreIds);
	        	//Set screenshotUrls
	        	ArrayList<String> screenshotUrls = new ArrayList<String>();
	        	JSONArray jsonScreenshotUrls = jsonObj.getJSONArray("screenshotUrls");
	        	for (int j = 0; j < jsonScreenshotUrls.length(); j++) {
	        		screenshotUrls.add((String) jsonScreenshotUrls.get(j));
	        	}
	        	software.setScreenshotUrls(screenshotUrls);
	        	//Set trackId
	        	software.setTrackId((int) jsonObj.getInt("trackId"));
	        	//Set trackContentRating
	        	software.setTrackContentRating((String) jsonObj.getString("trackContentRating"));
	        	//Set artworkUrl512
	        	software.setArtworkUrl512((String) jsonObj.getString("artworkUrl512"));
	        	//Set languageCodesISO2A
	        	ArrayList<String> languageCodesISO2A = new ArrayList<String>();
	        	JSONArray jsonLanguageCodesISO2A = jsonObj.getJSONArray("languageCodesISO2A");
	        	for (int j = 0; j < jsonLanguageCodesISO2A.length(); j++) {
	        		languageCodesISO2A.add((String) jsonLanguageCodesISO2A.get(j));
	        	}
	        	software.setLanguageCodesISO2A(languageCodesISO2A);
	        	//Set primaryGenreName
	        	software.setPrimaryGenreName((String) jsonObj.getString("primaryGenreName"));
	        	//Set sellerName
	        	software.setSellerName((String) jsonObj.getString("sellerName"));
	        	//Set supportedDevices
	        	software.setSupportedDevices((String) jsonObj.getString("supportedDevices"));
	        	//Set price
	        	software.setPrice(new Float(jsonObj.getString("price")));
	        	//Set artistViewUrl
	        	software.setArtistViewUrl((String) jsonObj.getString("artistViewUrl"));
	        	//Set bundleId
	        	software.setBundleId((String) jsonObj.getString("bundleId"));
	        	//Set artworkUrl100
	        	software.setArtworkUrl100((String) jsonObj.getString("artworkUrl100"));
	        	//Set artistName
	        	software.setArtistName((String) jsonObj.getString("artistName"));
	        	//Set fileSizeBytes
	        	software.setFileSizeBytes((int) jsonObj.getInt("fileSizeBytes"));
	        	//Set trackViewUrl
	        	software.setTrackViewUrl((String) jsonObj.getString("trackViewUrl"));
	        	//Set ipadScreenshotUrls
	        	ArrayList<String> ipadScreenshotUrls = new ArrayList<String>();
	        	JSONArray jsonIpadScreenshotUrls = jsonObj.getJSONArray("ipadScreenshotUrls");
	        	for (int j = 0; j < jsonIpadScreenshotUrls.length(); j++) {
	        		ipadScreenshotUrls.add((String) jsonIpadScreenshotUrls.get(j));
	        	}
	        	software.setIpadScreenshotUrls(ipadScreenshotUrls);
	        	
	        	try {
	        		//Set sellerUrl
		        	software.setSellerUrl((String) jsonObj.getString("sellerUrl"));
		        	//Set averageUserRatingForCurrentVersion
		        	software.setAverageUserRatingForCurrentVersion(new Float(jsonObj.getString("averageUserRatingForCurrentVersion")));
		        	//Set userRatingCount
		        	software.setUserRatingCount((String) jsonObj.getString("userRatingCount"));
		        	//Set userRatingCountForCurrentVersion
		        	software.setUserRatingCountForCurrentVersion((int) jsonObj.getInt("userRatingCountForCurrentVersion"));
		        	//Set averageUserRating
		        	software.setAverageUserRating(new Float(jsonObj.getString("averageUserRating")));
				} catch (Exception e) {
					// TODO: handle exception
				}
	        	
	        	//Add to list Software
	        	softwares.add(software);
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return softwares;
	}
	
	public static void main(String[] args) {
		ArrayList<Software> softwares = new ArrayList<Software>();
		softwares = CrawlerItuneJson.getJson("http://itunes.apple.com/search?term=doremon&country=vn&media=software");
		if (softwares.size() > 0) {
			SoftwareDebug.debug(softwares.get(0));
		}
	}
}
